package com.ias.agdis_r.activity.task;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseActivity;
import com.ias.agdis_r.activity.MainActivity;
import com.ias.agdis_r.adapter.QJMonitorExpandableAdapter;
import com.ias.agdis_r.bean.task.FLG10TaskBean;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 清洁监控按人员分组列表
 * Created by guoxr on 2015/12/2.
 */
public class QJMonitorExpandableActivity extends BaseActivity {
    private ActionBar actionBar;
    private ExpandableListView expListView;
    private QJMonitorExpandableAdapter adapter;
    private List<List<FLG10TaskBean>> childList = new ArrayList<>();
    private List<String> groupList = new ArrayList<>();
    private int lastPosition = -1;
    private TextView noInfo;
    private boolean isTem = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qj_expandable_monitor);
        actionBar = getActionBar();
        initactionBar();
        creatDate();
        setListAdapter();
        bindData();
    }

    private void creatDate() {
        groupList.clear();
        childList.clear();
        List<FLG10TaskBean> list = JavaBeanManager.getInstance()
                .deleteEndTask(JavaBeanManager.getInstance().getFLG10Taskinfo().getList());
        if (null != list) {
            Map<String, List<FLG10TaskBean>> taskMap = new HashMap<String, List<FLG10TaskBean>>();
            if (isTem) {
                for (FLG10TaskBean task : list) {
                    if (!taskMap.containsKey(task.getTem())) {
                        taskMap.put(task.getTem(), new ArrayList<FLG10TaskBean>());
                    }
                    taskMap.get(task.getTem()).add(task);
                }
            } else {
                for (FLG10TaskBean task : list) {
                    if (!taskMap.containsKey(task.getFln())) {
                        taskMap.put(task.getFln(), new ArrayList<FLG10TaskBean>());
                    }
                    taskMap.get(task.getFln()).add(task);
                }
            }
            groupList.addAll(taskMap.keySet());
            childList.addAll(taskMap.values());
        }
    }


    private void setListAdapter() {
        noInfo = (TextView) findViewById(R.id.tv_noInfo);
        expListView = (ExpandableListView) findViewById(R.id.expListView);
        adapter = new QJMonitorExpandableAdapter(QJMonitorExpandableActivity.this, groupList, childList);
        expListView.setAdapter(adapter);
        expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if (lastPosition == -1) {
                    expListView.expandGroup(groupPosition);
                    expListView.setSelectedGroup(groupPosition);
                    lastPosition = groupPosition;
                } else if (lastPosition == groupPosition) {
                    expListView.collapseGroup(lastPosition);
                    lastPosition = -1;
                } else {
                    expListView.collapseGroup(lastPosition);
                    expListView.expandGroup(groupPosition);
                    expListView.setSelectedGroup(groupPosition);
                    lastPosition = groupPosition;
                }
                return true;
            }
        });
    }

    private void bindData() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
        if (groupList.size() > 0) {
            noInfo.setVisibility(View.GONE);
        } else {
            noInfo.setVisibility(View.VISIBLE);
        }
    }

    private void initactionBar() {
        actionBar.setCustomView(LayoutInflater.from(QJMonitorExpandableActivity.this).inflate(R.layout.actionbar_qj_expandable_monitor, null));
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);
        ((TextView) findViewById(R.id.txt_title)).setText(getString(R.string.qjtem_title));
        ImageView back = (ImageView) findViewById(R.id.action_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        ImageView home = (ImageView) findViewById(R.id.action_home);
        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(QJMonitorExpandableActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        ImageView mswitch = (ImageView) findViewById(R.id.action_switch);
        mswitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTem = !isTem;
                adapter.setType(isTem);
                creatDate();
                bindData();
            }
        });
    }

    @Override
    protected void operateResponseSucess(String serviceId, BaseResponse response) {

    }

    @Override
    protected void updateUI(String tag) {
        if (tag.equals(MessageModel.ITTL)) {
            creatDate();
            bindData();
        }
    }
}
